var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftStyle:'width:140rpx;margin-left:172rpx; display:flex;justify-content: center;',
    rightStyle:'width:140rpx;margin-right:175rpx; display:flex; justify-content: center;',
    newsArray:[],
    which:true,
    id:'',
    informArray:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
   
    
    wx.request({
      url: app.globalData.baseUrl +'chat/platform',
      data:{
        pageNo:1,
        pageSize:20
      },
      method:'GET',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log(res)
        var resData=res.data.data.data
        for(var index=0;index<resData.length;index++){
          var content='informArray['+index+'].content'
          var time = 'informArray['+index+'].time'
          that.setData({
            [content]:resData[index].content,
            // [time]: resData[index].gmtCreate.substring(0, 10) + '  '+resData[index].gmtCreate.substring(11, 16)
            [time]:resData[index].gmtCreateFormat.substring(0,16)
          })
        }
      }
    })
  },
  toWhich:function(res){
    //console.log(res.detail.id)
    var id=res.detail.id;
    if(id=='online'){
      this.setData({
        which:true
      })
    }else if(id=='near'){
      this.setData({
        which: false
      })
    }
  },
  toChat:function(event){
    var id=event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../chat/chat?id='+id+'&attention=1',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.isLogin()
    var that=this
    wx.request({
      url: app.globalData.baseUrl+'chat/list',
      method:'GET',
      data:{
        pageNo:1,
        pageSize:30
      },
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log('list')
        console.log(res)
        var resData=res.data.data.data
        try{
          for (var index = 0; index < resData.length; index++) { 
          var avatar='newsArray['+index+'].avatar'
          var nickName='newsArray['+index+']nickName'
          var content='newsArray['+index+'].content'
          var date='newsArray['+index+'].date'
          var userSex='newsArray['+index+'].userSex'
          var id='newsArray['+index+'].id'
          var isNew='newsArray['+index+'].isNew'
          var name=resData[index].receiverUser.nickName
          if(name.length>8){
            name=name.substring(0,8)+'...'
          }else{
            name=name
          }
        
          var newNumber='newsArray['+index+'].newNumber'
          var number=resData[index].readCount
          var isRead=false
          if(number==0){
             
          }else if(number>0&&number<99){
              isRead=true
          }else if(number>99){
            number='99+'
            isRead = true
          }
            var avatarReceiver = resData[index].receiverUser.avatarUrl
            var avatarSender = resData[index].senderUser.avatarUrl
            var myUserId=wx.getStorageSync('userID')
            var receiverId = resData[index].receiverUser.id
            var senderId = resData[index].senderUser.id
            var sendName = resData[index].senderUser.nickName
            var receiveName = resData[index].receiverUser.nickName
            var a
            var id11
            var name11
            console.log(myUserId)
            console.log(receiverId)
            console.log(senderId)
            console.log(avatarReceiver)
            var sex
            if(myUserId==receiverId){
             
               id11=senderId
               name11=sendName
              a = avatarSender
              sex=resData[index].senderUser.gender
              // that.setData({
              //   id:id11
              // })
            }else if(myUserId==senderId){
             
              id11=receiverId
              a = avatarReceiver
              name11=receiveName
              sex = resData[index].receiverUser.gender
              // that.setData({
              //   id: id11
              // })
            }
            var newContent = resData[index].content
            if(newContent.length>18){
              newContent=newContent.substring(0,16)+'...'
            }
            var sexStr='an.png'
            if(sex==0){
              sexStr='an.png'
            }else if(sex==1){
              sexStr='v.png'
            }
          that.setData({
            [userSex]:sexStr,
            [isNew]:isRead,
            [content]: newContent,
            [date]:resData[index].gmtCreateFormat.substring(0,16),
            [id]:id11,
            [avatar]:a,
            [newNumber]:number,
            [nickName]:name11
          })
          }
        }catch(err){
          console.log(err)
        
        }
       
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})